//
//  CLChatVoiceCell.swift
//  Potato
//
//  Created by AUG on 2019/10/14.
//

import Lottie
import UIKit

class CLChatVoiceCell: CLChatCell {
    /// 背景气泡
    var bubbleImageView = UIImageView()
    /// 时间
    private lazy var durationLabel: UILabel = {
        let view = UILabel()
        view.font = .systemFont(ofSize: 13)
        view.textColor = .white
        return view
    }()

    /// 左侧播放动画
    lazy var leftPlayAnimation: LottieAnimationView = {
        let view = LottieAnimationView(animation: LottieAnimation.named("data-left"))
        view.loopMode = .loop
        view.backgroundBehavior = .pauseAndRestore
        return view
    }()

    /// 左侧播放动画
    lazy var rightPlayAnimation: LottieAnimationView = {
        let view = LottieAnimationView(animation: LottieAnimation.named("data-right"))
        view.loopMode = .loop
        view.backgroundBehavior = .pauseAndRestore
        return view
    }()

    /// 左侧气泡
    private lazy var leftBubbleImage: UIImage = {
        var image = UIImage(named: "leftBg")!
        image = image.resizableImage(withCapInsets: UIEdgeInsets(top: image.size.height * 0.3, left: image.size.width * 0.3, bottom: image.size.height * 0.3, right: image.size.width * 0.3))
        return image
    }()

    /// 右侧气泡
    private lazy var rightBubbleImage: UIImage = {
        var image = UIImage(named: "rightBg")!
        image = image.resizableImage(withCapInsets: UIEdgeInsets(top: image.size.height * 0.3, left: image.size.width * 0.3, bottom: image.size.height * 0.3, right: image.size.width * 0.3))
        return image
    }()
}

extension CLChatVoiceCell {
    override func initUI() {
        super.initUI()
        contentView.addSubview(bubbleImageView)
        contentView.addSubview(leftPlayAnimation)
        contentView.addSubview(rightPlayAnimation)
        contentView.addSubview(durationLabel)
        bubbleImageView.isUserInteractionEnabled = true
        let tapPhotoView = UITapGestureRecognizer(target: self, action: #selector(tapPhotoViewAction))
        bubbleImageView.addGestureRecognizer(tapPhotoView)
        bubbleImageView.isExclusiveTouch = true
    }

    override func makeConstraints() {
        super.makeConstraints()
        leftPlayAnimation.snp.makeConstraints { make in
            make.centerY.equalTo(bubbleImageView)
            make.size.equalTo(20)
            make.left.equalTo(bubbleImageView.snp.left).offset(10)
        }
        rightPlayAnimation.snp.makeConstraints { make in
            make.centerY.equalTo(bubbleImageView)
            make.size.equalTo(20)
            make.right.equalTo(bubbleImageView.snp.right).offset(-10)
        }
    }
}

extension CLChatVoiceCell {
    private func remakeConstraints(isFromMyself: Bool) {
        userIcon.snp.remakeConstraints { make in
            make.top.equalTo(10)
            make.size.equalTo(40)
            if isFromMyself {
                make.right.equalTo(-15)
            }else{
                make.left.equalTo(15)
            }
        }
        nameLab.snp.remakeConstraints { make in
            make.top.equalTo(userIcon)
            make.height.equalTo(20)
            if isFromMyself {
                make.right.equalTo(userIcon.snp.left).offset(-10)
            }else{
                make.left.equalTo(userIcon.snp.right).offset(10)
            }
            make.width.lessThanOrEqualTo(screenWidth - 150)
        }
        bubbleImageView.snp.remakeConstraints { make in
            if isFromMyself {
                make.right.equalTo(userIcon.snp.left).offset(-10)
            } else {
                make.left.equalTo(userIcon.snp.right).offset(10)
            }
            make.height.equalTo(40)
            make.width.equalTo(0)
            make.top.equalTo(nameLab.snp.bottom).offset(5)
//            make.centerY.equalToSuperview()
        }
        durationLabel.snp.remakeConstraints { make in
            make.centerY.equalTo(bubbleImageView)
            if isFromMyself {
                make.right.equalTo(bubbleImageView.snp.right).offset(-35)
            } else {
                make.left.equalTo(bubbleImageView.snp.left).offset(35)
            }
        }
        bottomContentView.snp.remakeConstraints { make in
            make.edges.equalTo(bubbleImageView)
        }
    }
    
    @objc private func tapPhotoViewAction() {
        if let item = item{
            routerEventWithName(kMessageVoiceKey, userInfo: [kMessageVoiceKey:item], self)
        }

    }
    
    /// 是否暂停语音动画
    func isPlayAnimal(_ isPlay: Bool){
        guard let item = item as? CLChatVoiceItem else { return  }
        if item.isFromMyself {
           isPlay ? rightPlayAnimation.play() : rightPlayAnimation.stop()
        }else{
           isPlay ? leftPlayAnimation.play() : leftPlayAnimation.stop()
        }
    }
    
}

extension CLChatVoiceCell: CLRowProtocol {
    func setItem(_ item: CLChatVoiceItem, indexPath: IndexPath) {
        self.item = item
        leftPlayAnimation.isHidden = item.isFromMyself
        rightPlayAnimation.isHidden = !item.isFromMyself
        bubbleImageView.image = item.isFromMyself ? rightBubbleImage : leftBubbleImage
        remakeConstraints(isFromMyself: item.isFromMyself)
        let duration = CGFloat(floor(Double(item.duration)))
        durationLabel.text = "\(Int(duration))"
        durationLabel.textColor = item.isFromMyself ? .white : .black
        var width: CGFloat = 5
        width = duration * width + 55
        width = max(min(width, screenWidth * 0.45), 70)
        bubbleImageView.snp.updateConstraints { make in
            make.width.equalTo(width)
        }
    }
}
